package com.huluobo.a2109kotlinshopping.login

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.huluobo.a2109kotlinshopping.base.BaseViewModel
import com.huluobo.a2109kotlinshopping.net.Const
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 *  Created by LC on 2024/1/10.
 */
class LoginViewModel : BaseViewModel() {
    private val repo = LoginRepo()
    val loginSuccess = MutableLiveData<UserBeanResult>()
    val loginFailed = MutableLiveData<String>()

    fun login(username: String, password: String) {
        //声明一个map,键值对都为String
        val map = mutableMapOf<String, String>()
        //给map中的username赋值
        map[Const.PARAM_USERNAME] = username
        //给map中的password赋值
        map[Const.PARAM_PASSWORD] = password
        //把map转换为一个json
        val json = Gson().toJson(map)
        //把json转换为一个body
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), json)
        //调用repo中的登录请求,传body和成功失败的LiveData
        repo.login(body, loginSuccess, loginFailed)
    }
}